
package com.hlkj.pay.app.merchant.ext.impl.huifu.ext;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.merchant.ext.dto.order.ChannelPayOrderQueryContext;
import com.hlkj.pay.app.merchant.ext.dto.order.ChannelRefundPayOrderContext;
import com.hlkj.pay.app.merchant.ext.impl.huifu.dto.HuiFuAppChannelParams;
import com.hlkj.pay.app.merchant.ext.impl.huifu.dto.HuiFuConfig;
import com.hlkj.pay.app.merchant.ext.impl.huifu.handler.params.HuiFuMerchantAccessCompanyParamsHandle;
import com.hlkj.pay.app.merchant.ext.impl.huifu.handler.params.HuiFuPayOrderRequestParamsHandler;
import com.hlkj.pay.app.merchant.ext.impl.huifu.handler.params.HuiFuPayOrderResponseParamsHandler;
import com.hlkj.pay.infrastructure.model.order.PayOrderDO;
import com.hlkj.pay.infrastructure.model.order.RefundOrderDO;
import com.hlkj.pay.infrastructure.model.pay.ProviderChannelConfigDO;
import com.hlkj.pay.util.DateUtils;
import com.huifu.bspay.sdk.opps.client.BasePayClient;
import com.huifu.bspay.sdk.opps.core.BasePay;
import com.huifu.bspay.sdk.opps.core.config.MerConfig;
import com.huifu.bspay.sdk.opps.core.request.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.Map;

/**
 * 汇付 正扫 反扫 通用处理
 * 
 * @author HlpayTeam
 * @date 2024/10/11 16:22
 */
@Slf4j
@Repository
@RequiredArgsConstructor
public class HuiFuSceneCommonPayOrder {

    /**
     * 汇付查询支付订单
     * 
     * @param channelPayOrderQueryContext
     */
    public void commonQueryChannelPayOrder(ChannelPayOrderQueryContext channelPayOrderQueryContext) {
        ProviderChannelConfigDO providerChannelConfigDO = channelPayOrderQueryContext.getProviderChannelConfigDO();
        String channelConfig = providerChannelConfigDO.getParamsConfig();
        // 配置转换
        HuiFuConfig huiFuConfig = JsonUtils.parseObject(channelConfig, HuiFuConfig.class);
        HuiFuAppChannelParams huiFuAppChannelParams = JsonUtils.parseObject(channelPayOrderQueryContext.getMerchantAppChannelDO().getAppConfigJson(), HuiFuAppChannelParams.class);
        PayOrderDO payOrderDO = channelPayOrderQueryContext.getPayOrderDO();
        try {
            // 1. 数据初始化
            MerConfig merConfig = HuiFuMerchantAccessCompanyParamsHandle.merConfig(huiFuConfig);
            BasePay.initWithMerConfig(merConfig);
            // 2.组装请求参数
            V2TradePaymentScanpayQueryRequest request = new V2TradePaymentScanpayQueryRequest();
            // 原机构请求日期
            request.setOrgReqDate(DateUtils.dateToString(payOrderDO.getCreateTime(), "yyyyMMdd"));
            // 商户号
            request.setHuifuId(huiFuAppChannelParams.getHuiFuId());
            // 商户订单号线下POS、扫码机具发起的交易需要提供；&lt;font
            // color&#x3D;&quot;green&quot;&gt;示例值：22577563652260773965&lt;/font&gt;
            // request.setMerOrdId("");
            // 交易返回的全局流水号org_hf_seq_id，org_req_seq_id，out_trans_id，party_order_id四选一；&lt;br/&gt;&lt;font
            // color&#x3D;&quot;green&quot;&gt;示例值：00290TOP1GR210919004230P853ac13262200000&lt;/font&gt;
            // request.setOrgHfSeqId("test");
            // 原机构请求流水号org_hf_seq_id，org_req_seq_id，out_trans_id，party_order_id四选一；&lt;br/&gt;&lt;font
            // color&#x3D;&quot;green&quot;&gt;示例值：202110210012100005&lt;/font&gt;
            // 服务订单创建请求流水号
            request.setOrgReqSeqId(payOrderDO.getPayOrderNo());
            // request.setOrgHfSeqId(payOrderDO.getChannelOrderId());
            // 用户账单上的交易订单号org_hf_seq_id，org_req_seq_id，out_trans_id，party_order_id四选一；&lt;br/&gt;&lt;font
            // color&#x3D;&quot;green&quot;&gt;示例值：092021091922001451301445517582&lt;/font&gt;；参见[用户账单说明](https://paas.huifu.com/partners/api/#/czsm/api_czsm_yhzd)
            // request.setOutTransId("2022110722001473401435491529");
            // 用户账单上的商户订单号org_hf_seq_id，org_req_seq_id，out_trans_id，party_order_id四选一；&lt;br/&gt;&lt;font
            // color&#x3D;&quot;green&quot;&gt;示例值：03232109190255105603561&lt;/font&gt;；参见[用户账单说明](https://paas.huifu.com/partners/api/#/czsm/api_czsm_yhzd)
            // request.setPartyOrderId(payOrderDO.getChannelOrderNo());
            // 3. 发起API调用
            Map<String, Object> response = BasePayClient.request(request, false);
            HuiFuPayOrderResponseParamsHandler.processQueryOrderResultData(response, channelPayOrderQueryContext);
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("支付查询支付订单请求失败 message:{}", e.getMessage(), e);
            channelPayOrderQueryContext.setRemark("斗拱查询支付订单请求失败,失败信息" + e.getMessage());
            channelPayOrderQueryContext.setProcessStatus(Boolean.FALSE);
            // channelMerchantAccessQueryContext.setAccessStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.APPROVE_REFUSE);
        }
    }

    /**
     * 汇付关闭订单
     * 
     * @param channelPayOrderQueryContext
     */
    public void commonCloseChannelPayOrder(ChannelPayOrderQueryContext channelPayOrderQueryContext) {
        ProviderChannelConfigDO providerChannelConfigDO = channelPayOrderQueryContext.getProviderChannelConfigDO();
        String channelConfig = providerChannelConfigDO.getParamsConfig();
        // 配置转换
        HuiFuConfig huiFuConfig = JsonUtils.parseObject(channelConfig, HuiFuConfig.class);
        HuiFuAppChannelParams huiFuAppChannelParams = JsonUtils.parseObject(channelPayOrderQueryContext.getMerchantAppChannelDO().getAppConfigJson(), HuiFuAppChannelParams.class);
        try {
            // 1. 数据初始化
            MerConfig merConfig = HuiFuMerchantAccessCompanyParamsHandle.merConfig(huiFuConfig);
            BasePay.initWithMerConfig(merConfig);
            V2TradePaymentScanpayCloseRequest request = HuiFuPayOrderRequestParamsHandler.v2TradePaymentScanpayCloseRequest(channelPayOrderQueryContext);
            // 汇付客户Id
            request.setHuifuId(huiFuAppChannelParams.getHuiFuId());
            // 3. 发起API调用
            Map<String, Object> response = BasePayClient.request(request, false);
            log.info("斗拱关闭支付订单返回信息response:{}", JsonUtils.toJsonString(response));
            HuiFuPayOrderResponseParamsHandler.processCloseOrderResultData(response, channelPayOrderQueryContext);
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("支付关闭支付订单请求失败 message:{}", e.getMessage(), e);
            channelPayOrderQueryContext.setRemark("斗拱关闭支付订单请求失败,失败信息" + e.getMessage());
            channelPayOrderQueryContext.setProcessStatus(Boolean.FALSE);
            // channelMerchantAccessQueryContext.setAccessStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.APPROVE_REFUSE);
        }
    }

    /**
     * 汇付查询关闭订单状态
     * 
     * @param channelPayOrderQueryContext
     */
    public void commonQueryChannelClosePayOrder(ChannelPayOrderQueryContext channelPayOrderQueryContext) {
        ProviderChannelConfigDO providerChannelConfigDO = channelPayOrderQueryContext.getProviderChannelConfigDO();
        String channelConfig = providerChannelConfigDO.getParamsConfig();
        // 配置转换
        HuiFuConfig huiFuConfig = JsonUtils.parseObject(channelConfig, HuiFuConfig.class);
        HuiFuAppChannelParams huiFuAppChannelParams = JsonUtils.parseObject(channelPayOrderQueryContext.getMerchantAppChannelDO().getAppConfigJson(), HuiFuAppChannelParams.class);
        try {
            // 1. 数据初始化
            MerConfig merConfig = HuiFuMerchantAccessCompanyParamsHandle.merConfig(huiFuConfig);
            BasePay.initWithMerConfig(merConfig);
            V2TradePaymentScanpayClosequeryRequest request = HuiFuPayOrderRequestParamsHandler.v2TradePaymentScanpayClosequeryRequest(channelPayOrderQueryContext);
            // 汇付客户Id
            request.setHuifuId(huiFuAppChannelParams.getHuiFuId());
            // 3. 发起API调用
            Map<String, Object> response = BasePayClient.request(request, false);
            log.info("斗拱查询关闭支付订单返回信息response:{}", JsonUtils.toJsonString(response));
            HuiFuPayOrderResponseParamsHandler.processCloseOrderResultData(response, channelPayOrderQueryContext);
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("支付查询关闭支付订单请求失败 message:{}", e.getMessage(), e);
            channelPayOrderQueryContext.setRemark("斗拱查询关闭支付订单请求失败,失败信息" + e.getMessage());
            channelPayOrderQueryContext.setProcessStatus(Boolean.FALSE);
            // channelMerchantAccessQueryContext.setAccessStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.APPROVE_REFUSE);
        }
    }

    /**
     * 汇付订单退款
     * 
     * @param channelRefundPayOrderContext
     */
    public void commonRefundChannelPayOrder(ChannelRefundPayOrderContext channelRefundPayOrderContext) {
        ProviderChannelConfigDO providerChannelConfigDO = channelRefundPayOrderContext.getProviderChannelConfigDO();
        String channelConfig = providerChannelConfigDO.getParamsConfig();
        // 配置转换
        HuiFuConfig huiFuConfig = JsonUtils.parseObject(channelConfig, HuiFuConfig.class);
        HuiFuAppChannelParams huiFuAppChannelParams = JsonUtils.parseObject(channelRefundPayOrderContext.getMerchantAppChannelDO().getAppConfigJson(), HuiFuAppChannelParams.class);
        try {
            // 1. 数据初始化
            MerConfig merConfig = HuiFuMerchantAccessCompanyParamsHandle.merConfig(huiFuConfig);
            BasePay.initWithMerConfig(merConfig);
            V2TradePaymentScanpayRefundRequest request = HuiFuPayOrderRequestParamsHandler.v2TradePaymentScanpayRefundRequest(channelRefundPayOrderContext);
            // 汇付客户Id
            request.setHuifuId(huiFuAppChannelParams.getHuiFuId());
            log.info("斗拱退款支付订单请求信息request:{}", JsonUtils.toJsonString(request));
            // 3. 发起API调用
            Map<String, Object> response = BasePayClient.request(request, false);
            log.info("斗拱退款支付订单返回信息response:{}", JsonUtils.toJsonString(response));
            HuiFuPayOrderResponseParamsHandler.processRefundOrderResultData(response, channelRefundPayOrderContext);
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("支付退款支付订单请求失败 message:{}", e.getMessage(), e);
            channelRefundPayOrderContext.setRemark("斗拱退款支付订单请求失败,失败信息" + e.getMessage());
            channelRefundPayOrderContext.setProcessStatus(Boolean.FALSE);
        }
    }

    /**
     * 汇付订单退款状态查询
     * 
     * @param channelRefundPayOrderContext
     */
    public void commonQueryRefundChannelPayOrder(ChannelRefundPayOrderContext channelRefundPayOrderContext) {
        ProviderChannelConfigDO providerChannelConfigDO = channelRefundPayOrderContext.getProviderChannelConfigDO();
        String channelConfig = providerChannelConfigDO.getParamsConfig();
        RefundOrderDO refundOrderDO = channelRefundPayOrderContext.getRefundOrderDO();
        // 配置转换
        HuiFuConfig huiFuConfig = JsonUtils.parseObject(channelConfig, HuiFuConfig.class);
        HuiFuAppChannelParams huiFuAppChannelParams = JsonUtils.parseObject(channelRefundPayOrderContext.getMerchantAppChannelDO().getAppConfigJson(), HuiFuAppChannelParams.class);
        try {
            // 1. 数据初始化
            MerConfig merConfig = HuiFuMerchantAccessCompanyParamsHandle.merConfig(huiFuConfig);
            BasePay.initWithMerConfig(merConfig);
            // 2.组装请求参数
            V2TradePaymentScanpayRefundqueryRequest request = new V2TradePaymentScanpayRefundqueryRequest();
            // 商户号
            request.setHuifuId(huiFuAppChannelParams.getHuiFuId());
            // 退款请求日期

            request.setOrgReqDate(DateUtils.dateToString(refundOrderDO.getCreateTime(), "yyyyMMdd"));
            // 退款全局流水号退款请求流水号,退款全局流水号,终端订单号三选一不能都为空；&lt;br/&gt;&lt;font
            // color&#x3D;&quot;green&quot;&gt;示例值：0030default220825182711P099ac1f343f00000&lt;/font&gt;
            // request.setOrgHfSeqId("003100TOP2B221110093241P139ac139c0c00000");
            // 退款请求流水号退款请求流水号,退款全局流水号,终端订单号三选一不能都为空；&lt;br/&gt;&lt;font
            // color&#x3D;&quot;green&quot;&gt;示例值：202110210012100005&lt;/font&gt;
            request.setOrgReqSeqId(refundOrderDO.getRefundOrderNo());
            // 终端订单号退款请求流水号,退款全局流水号,终端订单号三选一不能都为空；&lt;br/&gt;&lt;font
            // color&#x3D;&quot;green&quot;&gt;示例值：16672670833524393&lt;/font&gt;
            // request.setMerOrdId("");
            // 3. 发起API调用
            Map<String, Object> response = BasePayClient.request(request, false);
            log.info("斗拱退款查询返回信息response:{}", JsonUtils.toJsonString(response));
            HuiFuPayOrderResponseParamsHandler.processRefundOrderResultData(response, channelRefundPayOrderContext);
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("支付退款查询请求失败 message:{}", e.getMessage(), e);
            channelRefundPayOrderContext.setRemark("斗拱退款查询请求失败,失败信息" + e.getMessage());
            channelRefundPayOrderContext.setProcessStatus(Boolean.FALSE);
        }
    }
}
